package com.maezia.tdr.dao.repository;

import com.maezia.tdr.common.CommonConstant;
import com.maezia.tdr.common.redis.BaseRedisRepository;
import com.maezia.tdr.dao.mapper.TestDriveReportMapper;
import com.maezia.tdr.domain.po.TestDriveReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import java.util.concurrent.TimeUnit;

@Repository
public class TestDriveReportRepository extends BaseRedisRepository<TestDriveReport> {

    private static String TEST_DRIVE_REPORT_KEY = "TEST_DRIVE_REPORT";

    @Autowired
    private TestDriveReportMapper testDriveReportMapper;

    public TestDriveReportRepository() {
        super(TestDriveReport.class);
    }

    public void save(TestDriveReport reportInfo){
        String redisKey = TEST_DRIVE_REPORT_KEY + CommonConstant.COLON + reportInfo.getMd5Value();
        if(!redisTemplateTemporary.hasKey(redisKey)){
            redisValueOpsTemporary.set(redisKey, reportInfo, 30, TimeUnit.DAYS);
        }
    }

    public TestDriveReport getByMd5(String md5) {
        String redisKey = TEST_DRIVE_REPORT_KEY + CommonConstant.COLON + md5;
        TestDriveReport reportInfo = redisValueOpsTemporary.get(redisKey);
        if(ObjectUtils.isEmpty(reportInfo)){
            reportInfo = testDriveReportMapper.getByMd5(md5);
            if(!ObjectUtils.isEmpty(reportInfo)){
                save(reportInfo);
            }
        }
        return reportInfo;
    }
}
